package communication.impl;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

import communication.NetworkService;

public class Sender {

	public void register(String name, String port) throws RemoteException {
		CallbackServerObject callbackServerObject = 
				new CallbackServerObject(name);
		try {
			int clientPort = Integer.parseInt(
				System.getProperty("protocole.callback.client.port", port));
			int registryPort = Integer.parseInt(
				System.getProperty("protocole.callback.registry.port", "33001"));
			String registryHost =
				System.getProperty("protocole.callback.registry.host", "127.0.0.1");
			String networkServiceBindingName =
					System.getProperty("protocole.callback.binding.name", 
							NetworkService.AdminNetworkServiceBindingName);

			System.out.println("Exporting client side callback object on " + clientPort);
			
			UnicastRemoteObject.exportObject(callbackServerObject, clientPort);
			
			System.out.println("Gets the registry over there: " 
						+ registryHost + ":" + registryPort);
			Registry remoteRegistry = 
					LocateRegistry.getRegistry(registryHost, registryPort);
			
			System.out.println("Lookup reference of object registered with " +  
										networkServiceBindingName);
			NetworkService remoteService = (NetworkService)
					remoteRegistry.lookup(networkServiceBindingName);
			
			System.out.println("Ready to register.");
			remoteService.register(callbackServerObject);
			
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw e;
		} catch (NotBoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}

